package vsvm.gui.forms;

import java.io.Serializable;
import javax.swing.JPanel;

import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.BorderLayout;
import javax.swing.JDialog;
import java.awt.GridBagLayout;
import javax.swing.JRadioButton;
import java.awt.GridBagConstraints;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.border.TitledBorder;

import vsvm.data.filter.ranking.rankingStuff.Threshold;

import java.awt.Insets;

public class ThresholdForm extends JDialog implements Serializable {

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JPanel jPanel = null;

	private JRadioButton rbtThreshold = null;

	private JRadioButton rbtLeave = null;

	private JLabel jLabel = null;

	private JLabel jLabel1 = null;

	private JTextField txtThreshold = null;

	private JTextField txtLeave = null;

	private JPanel jPanel1 = null;

	private JButton btnOk = null;

	private JButton btnCancel = null;
	
	private ThresholdForm me = null;
	private Threshold thresh = null;

	/**
	 * @param owner
	 */
	public ThresholdForm(Frame owner, Threshold t) {
		super(owner);
		setLocationRelativeTo(owner);
		thresh = t;
		initialize();
		me = this;
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setTitle("Threshold form");
		this.setSize(300, 200);
		this.setContentPane(getJContentPane());
		this.setVisible(true);
		if(thresh.isThreshold())
			rbtThreshold.setSelected(true);
		else
			rbtLeave.setSelected(true);
		txtThreshold.setText(String.valueOf(thresh.getThresholdValue()));
		txtLeave.setText(String.valueOf(thresh.getLeave()));
		
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJPanel1(), BorderLayout.SOUTH);
			jContentPane.add(getJPanel(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.fill = GridBagConstraints.BOTH;
			gridBagConstraints5.gridy = 1;
			gridBagConstraints5.weightx = 1.0;
			gridBagConstraints5.insets = new Insets(0, 20, 5, 20);
			gridBagConstraints5.gridx = 2;
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints4.gridy = 0;
			gridBagConstraints4.weightx = 1.0;
			gridBagConstraints4.insets = new Insets(0, 20, 5, 20);
			gridBagConstraints4.gridx = 2;
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.gridx = 1;
			gridBagConstraints3.insets = new Insets(0, 10, 5, 10);
			gridBagConstraints3.gridy = 1;
			jLabel1 = new JLabel();
			jLabel1.setText("Leave most important:");
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 1;
			gridBagConstraints2.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints2.insets = new Insets(0, 10, 5, 10);
			gridBagConstraints2.gridy = 0;
			jLabel = new JLabel();
			jLabel.setText("Threshold:");
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.gridx = 0;
			gridBagConstraints1.insets = new Insets(0, 0, 5, 0);
			gridBagConstraints1.gridy = 1;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.insets = new Insets(0, 0, 5, 0);
			gridBagConstraints.gridy = 0;
			jPanel = new JPanel();
			jPanel.setLayout(new GridBagLayout());
			jPanel.add(getRbtThreshold(), gridBagConstraints);
			jPanel.add(getRbtLeave(), gridBagConstraints1);
			jPanel.add(jLabel, gridBagConstraints2);
			jPanel.add(jLabel1, gridBagConstraints3);
			jPanel.add(getTxtThreshold(), gridBagConstraints4);
			jPanel.add(getTxtLeave(), gridBagConstraints5);
			jPanel.setBorder(BorderFactory.createTitledBorder(null, "Threshold", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
		}
		return jPanel;
	}

	/**
	 * This method initializes rbtThreshold	
	 * 	
	 * @return javax.swing.JRadioButton	
	 */
	private JRadioButton getRbtThreshold() {
		if (rbtThreshold == null) {
			rbtThreshold = new JRadioButton();
			rbtThreshold.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					rbtThreshold.setSelected(true);
					rbtLeave.setSelected(false);
				}
			});
		}
		return rbtThreshold;
	}

	/**
	 * This method initializes rbtLeave	
	 * 	
	 * @return javax.swing.JRadioButton	
	 */
	private JRadioButton getRbtLeave() {
		if (rbtLeave == null) {
			rbtLeave = new JRadioButton();
			rbtLeave.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					rbtThreshold.setSelected(false);
					rbtLeave.setSelected(true);
				}
			});
		}
		return rbtLeave;
	}

	/**
	 * This method initializes txtThreshold	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTxtThreshold() {
		if (txtThreshold == null) {
			txtThreshold = new JTextField();
		}
		return txtThreshold;
	}

	/**
	 * This method initializes txtLeave	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTxtLeave() {
		if (txtLeave == null) {
			txtLeave = new JTextField();
		}
		return txtLeave;
	}

	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			FlowLayout flowLayout = new FlowLayout();
			flowLayout.setHgap(16);
			jPanel1 = new JPanel();
			jPanel1.setLayout(flowLayout);
			jPanel1.add(getBtnOk(), null);
			jPanel1.add(getBtnCancel(), null);
		}
		return jPanel1;
	}

	/**
	 * This method initializes btnOk	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnOk() {
		if (btnOk == null) {
			btnOk = new JButton();
			btnOk.setText("   Ok   ");
			btnOk.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if(rbtThreshold.isSelected()){
						thresh.setThreshold(true);
						try {
							thresh.setThresholdValue(Double.parseDouble(txtThreshold.getText()));
						} catch (NumberFormatException e1) {
							JOptionPane.showMessageDialog(me, "Please, enter correct threshold.", "Error", JOptionPane.ERROR_MESSAGE);
						}
					} else {
						thresh.setThreshold(false);
						try {
							thresh.setLeave(Integer.parseInt(txtLeave.getText()));
						} catch (NumberFormatException e1) {
							JOptionPane.showMessageDialog(me, "Incorrect the most important attribute number.", "Error", JOptionPane.ERROR_MESSAGE);
						}
					}
					me.dispose();
				}
			});
		}
		return btnOk;
	}

	/**
	 * This method initializes btnCancel	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnCancel() {
		if (btnCancel == null) {
			btnCancel = new JButton();
			btnCancel.setText("Cancel");
			btnCancel.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					me.dispose();
				}
			});
		}
		return btnCancel;
	}

}  //  @jve:decl-index=0:visual-constraint="232,20"
